const db = wx.cloud.database()
const _ = db.command
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  id:"",
	  members:[],
	  leader:{},
	  myself:false
  },
	deleteMember(e){
		var that = this
		let memberid = e.currentTarget.dataset.memberid
		let index = e.currentTarget.dataset.index
		console.log(index)
		wx.showModal({
			title: '提示',
			content: '确定删除该队友么',
			success(res) {
				if (res.confirm) {
					console.log('用户点击确定')
					db.collection("team").doc(that.data.id).update({
						data:{
							teamPeople: _.pull(memberid)
						}
					}).then(res=>{
						wx.showToast({
							title: '删除成功',
							icon:"none",
							duration:1000
						})
						let members = that.data.members
						 members.splice(index, 1)
						that.setData({
							members:members
						})
					})
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	//   console.log(options.id)
	  this.setData({
		  id:options.id
	  })
	  //获取队长和队员信息
	  let memberid = []
	  let leaderid = ""
	  db.collection("team").doc(options.id).field({
		  teamPeople:true
	  }).get().then(aes=>{
		//   console.log(res.data)
			leaderid = aes.data.teamPeople[0],
			memberid = aes.data.teamPeople.slice(1, aes.data.teamPeople.length)
		  db.collection("users").where({
			  _openid: _.in(memberid)
		  }).get().then(res => {
			  this.setData({
				  members: res.data
			  })
		  })
		  db.collection("users").where({
			  _openid: leaderid
		  }).get().then(res => {
			  this.setData({
				  leader: res.data[0]
			  })
		  })
		  //判断是否创建者打开
		  if (app.globalData.openid == leaderid) {
			  this.setData({
				  myself: true
			  })
		  }
	  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})